package com.john.apptest1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.bluetooth.*;
import android.content.Intent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private int REQUEST_ENABLE_BT = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testBlueTooth();
    }

    private void testBlueTooth(){

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null){
            //表明此手机不支持蓝牙
            Toast.makeText(MainActivity.this, "can not" , Toast.LENGTH_LONG).show();
            return;
        }


        if(!mBluetoothAdapter.isEnabled()){	//蓝牙未开启，则开启蓝牙
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == REQUEST_ENABLE_BT){
            if(requestCode == RESULT_OK){
                //蓝牙已经开启
                Toast.makeText(MainActivity.this, "open" , Toast.LENGTH_LONG).show();
            }
        }
    }
}
